import {LANGUAGE_TYPE} from "../type/LanguageType";
import {translate} from "./Translate";
// 获取 Vuex Store 实例
import store from '../store/index';
import {CONFIG} from "../store/mutation-types";

/**
 * 去重
 */
export function reduceRepetitionRate(appId, password, level, from, q, successCallback, errorCallback) {
  if (level <= 0) {
    return q
  }
  reduceRepetition(appId, password, level, from, q,
    (qNow, toNow) => {
      translate(appId, password, toNow, qNow, from,
        (data) => {
          successCallback(data)
        },
        (data) => {
          errorCallback(data)
        })
    })
}

function reduceRepetition(appId, password, level, from, q, callback) {
  if (level <= 0) {
    callback(q, from)
    return
  }

  let index = Math.round(Math.random() * LANGUAGE_TYPE.length)
  let to = LANGUAGE_TYPE[index]

  translate(appId, password, from, q, to,
    (data) => {
      console.log(data)
      store.commit(CONFIG.UPDATE_LOG_MSG, JSON.stringify(data))
      reduceRepetition(appId, password, level - 1, to, data.trans_result[0].dst, callback)
    },
    (data) => {
      console.log(data)
      store.commit(CONFIG.UPDATE_LOG_MSG, JSON.stringify(data))
    })
}